import Mock from 'mockjs';
import { MockParams } from '@/types/mock';
import setupMock, { successResponseWrap, failResponseWrap } from '@/utils/setup-mock';
import { isLogin } from '@/utils/auth';

const menuList = [
  {
    id: 1,
    name: '仪表盘',
    path: '/dashboard',
    icon: 'icon-dashboard',
    parentId: null,
    order: 1,
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    children: [
      {
        id: 2,
        name: '工作台',
        path: '/dashboard/workplace',
        icon: 'icon-apps',
        parentId: 1,
        order: 1,
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
      },
      {
        id: 3,
        name: '监控页',
        path: '/dashboard/monitor',
        icon: 'icon-computer',
        parentId: 1,
        order: 2,
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
      },
    ],
  },
  {
    id: 4,
    name: '系统管理',
    path: '/system',
    icon: 'icon-settings',
    parentId: null,
    order: 7,
    status: 1,
    createTime: '2023-01-01 00:00:00',
    updateTime: '2023-01-01 00:00:00',
    children: [
      {
        id: 5,
        name: '菜单管理',
        path: '/system/menu',
        icon: 'icon-menu',
        parentId: 4,
        order: 1,
        status: 1,
        createTime: '2023-01-01 00:00:00',
        updateTime: '2023-01-01 00:00:00',
      },
    ],
  },
];

setupMock({
  setup() {
    console.log('设置菜单 Mock 数据');
    
    // 获取菜单列表
    Mock.mock(new RegExp('/api/menu/list'), (params: MockParams) => {
      return successResponseWrap(menuList);
    });

    // 添加菜单
    Mock.mock(new RegExp('/api/menu/add'), 'post', (params: MockParams) => {
      const { body } = params;
      const newMenu = {
        ...JSON.parse(body),
        id: Mock.Random.id(),
        createTime: Mock.Random.datetime(),
        updateTime: Mock.Random.datetime(),
      };
      return successResponseWrap(newMenu);
    });

    // 更新菜单
    Mock.mock(new RegExp('/api/menu/update'), 'put', () => {
      return successResponseWrap(true);
    });

    // 删除菜单
    Mock.mock(new RegExp('/api/menu/delete/.*'), 'delete', () => {
      return successResponseWrap(true);
    });

    // 菜单接口 - /api/menu/user
    Mock.mock(new RegExp('/api/menu/user'), () => {
      console.log('Mock: 拦截到 /api/menu/user 请求');
      if (isLogin()) {
        return {
          code: 200,
          message: '操作成功',
          data: menuList
        };
      }
      return failResponseWrap(null, '未登录', 50008);
    });

    console.log('菜单 Mock 数据设置完成');
  },
}); 